D:\git\skunkworks\herald-for-cpp\herald-tests\advertparser-tests.cpp
Line | Count | Source |
1 | | // Copyright 2020-2021 Herald Project Contributors |
2 | | // SPDX-License-Identifier: Apache-2.0 |
3 | | // |
4 | | |
5 | | #include <memory> |
6 | | #include <vector> |
7 | | |
8 | | #include "catch.hpp" |
9 | | |
10 | | #include "herald/herald.h" |
11 | | |
12 | 1 | TEST_CASE("advert-parser-subdatabigendian", "[advert][parser][subdatabigendian]") { |
13 | 1 | SECTION("advert-parser-subdatabigendian") { |
14 | 1 | std::uint8_t data[] {0,1,5,6,7,8,12,13,14}; |
15 | 1 | herald::datatype::Data original(data,9); |
16 | 1 | REQUIRE(std::byte(5) == original.at(2)); |
17 | 1 | REQUIRE(std::byte(6) == original.at(3)); |
18 | 1 | REQUIRE(std::byte(7) == original.at(4)); |
19 | 1 | REQUIRE(std::byte(8) == original.at(5)); |
20 | 1 | herald::datatype::Data result = |
21 | 1 | herald::ble::filter::BLEAdvertParser::subDataBigEndian(original,2,4); |
22 | 1 | REQUIRE(result.size() == 4); |
23 | 1 | REQUIRE(std::byte(5) == result.at(0)); |
24 | 1 | REQUIRE(std::byte(6) == result.at(1)); |
25 | 1 | REQUIRE(std::byte(7) == result.at(2)); |
26 | 1 | REQUIRE(std::byte(8) == result.at(3)); |
27 | 1 | } |
28 | 1 | } |
29 | 1 | TEST_CASE("advert-parser-subdatalittleendian", "[advert][parser][subdatalittleendian]") { |
30 | 1 | SECTION("advert-parser-subdatalittleendian") { |
31 | 1 | std::uint8_t data[] {0,1,5,6,7,8,12,13,14}; |
32 | 1 | herald::datatype::Data original(data, 9); |
33 | 1 | herald::datatype::Data result = |
34 | 1 | herald::ble::filter::BLEAdvertParser::subDataLittleEndian(original,2,4); |
35 | 1 | REQUIRE(result.size() == 4); |
36 | 1 | REQUIRE(std::byte(8) == result.at(0)); |
37 | 1 | REQUIRE(std::byte(7) == result.at(1)); |
38 | 1 | REQUIRE(std::byte(6) == result.at(2)); |
39 | 1 | REQUIRE(std::byte(5) == result.at(3)); |
40 | 1 | } |
41 | 1 | } |
42 | | |
43 | 1 | TEST_CASE("advert-parser-subdatabigendianoverflow", "[advert][parser][subdatabigendianoverflow]") { |
44 | 1 | SECTION("advert-parser-subdatabigendianoverflow") { |
45 | 1 | std::uint8_t data[] {0,1,5,6,7}; |
46 | 1 | herald::datatype::Data original(data, 5); |
47 | 1 | herald::datatype::Data result = |
48 | 1 | herald::ble::filter::BLEAdvertParser::subDataBigEndian(original,2,4); |
49 | 1 | REQUIRE(3 == result.size()); |
50 | 1 | } |
51 | 1 | } |
52 | | |
53 | 1 | TEST_CASE("advert-parser-subdatalittleendianoverflow", "[advert][parser][subdatalittleendianoverflow]") { |
54 | 1 | SECTION("advert-parser-subdatalittleendianoverflow") { |
55 | 1 | std::uint8_t data[] {0,1,5,6,7}; |
56 | 1 | herald::datatype::Data original(data, 5); |
57 | 1 | herald::datatype::Data result = |
58 | 1 | herald::ble::filter::BLEAdvertParser::subDataLittleEndian(original,2,4); |
59 | 1 | REQUIRE(3 == result.size()); |
60 | 1 | } |
61 | 1 | } |
62 | | |
63 | 1 | TEST_CASE("advert-parser-subdatabigendianlowindex", "[advert][parser][subdatabigendianlowindex]") { |
64 | 1 | SECTION("advert-parser-subdatabigendianlowindex") { |
65 | 1 | std::uint8_t data[] {0,1,5,6,7}; |
66 | 1 | herald::datatype::Data original(data, 5); |
67 | 1 | herald::datatype::Data result = |
68 | 1 | herald::ble::filter::BLEAdvertParser::subDataBigEndian(original,-1,4); |
69 | 1 | REQUIRE(0 == result.size()); |
70 | 1 | } |
71 | 1 | } |
72 | | |
73 | 1 | TEST_CASE("advert-parser-subdatalittleendianlowindex", "[advert][parser][subdatalittleendianlowindex]") { |
74 | 1 | SECTION("advert-parser-subdatalittleendianlowindex") { |
75 | 1 | std::uint8_t data[] {0,1,5,6,7}; |
76 | 1 | herald::datatype::Data original(data, 5); |
77 | 1 | herald::datatype::Data result = |
78 | 1 | herald::ble::filter::BLEAdvertParser::subDataLittleEndian(original,-1,4); |
79 | 1 | REQUIRE(0 == result.size()); |
80 | 1 | } |
81 | 1 | } |
82 | | |
83 | 1 | TEST_CASE("advert-parser-subdatabigendianhighindex", "[advert][parser][subdatabigendianhighindex]") { |
84 | 1 | SECTION("advert-parser-subdatabigendianhighindex") { |
85 | 1 | std::uint8_t data[] {0,1,5,6,7}; |
86 | 1 | herald::datatype::Data original(data, 5); |
87 | 1 | herald::datatype::Data result = |
88 | 1 | herald::ble::filter::BLEAdvertParser::subDataBigEndian(original,5,4); |
89 | 1 | REQUIRE(0 == result.size()); |
90 | 1 | } |
91 | 1 | } |
92 | | |
93 | 1 | TEST_CASE("advert-parser-subdatalittleendianhighindex", "[advert][parser][subdatalittleendianhighindex]") { |
94 | 1 | SECTION("advert-parser-subdatalittleendianhighindex") { |
95 | 1 | std::uint8_t data[] {0,1,5,6,7}; |
96 | 1 | herald::datatype::Data original(data, 5); |
97 | 1 | herald::datatype::Data result = |
98 | 1 | herald::ble::filter::BLEAdvertParser::subDataLittleEndian(original,5,4); |
99 | 1 | REQUIRE(0 == result.size()); |
100 | 1 | } |
101 | 1 | } |
102 | | |
103 | 1 | TEST_CASE("advert-parser-subdatabigendianlargelength", "[advert][parser][subdatabigendianlargelength]") { |
104 | 1 | SECTION("advert-parser-subdatabigendianlargelength") { |
105 | 1 | std::uint8_t data[] {0,1,5,6,7}; |
106 | 1 | herald::datatype::Data original(data, 5); |
107 | 1 | herald::datatype::Data result = |
108 | 1 | herald::ble::filter::BLEAdvertParser::subDataBigEndian(original,2,4); |
109 | 1 | REQUIRE(3 == result.size()); |
110 | 1 | } |
111 | 1 | } |
112 | | |
113 | 1 | TEST_CASE("advert-parser-subdatalittleendianlargelength", "[advert][parser][subdatalittleendianlargelength]") { |
114 | 1 | SECTION("advert-parser-subdatalittleendianlargelength") { |
115 | 1 | std::uint8_t data[] {0,1,5,6,7}; |
116 | 1 | herald::datatype::Data original(data, 5); |
117 | 1 | herald::datatype::Data result = |
118 | 1 | herald::ble::filter::BLEAdvertParser::subDataLittleEndian(original,2,4); |
119 | 1 | REQUIRE(3 == result.size()); |
120 | 1 | } |
121 | 1 | } |
122 | | |
123 | 1 | TEST_CASE("advert-parser-subdatabigendianemptydata", "[advert][parser][subdatabigendianemptydata]") { |
124 | 1 | SECTION("advert-parser-subdatabigendianemptydata") { |
125 | 1 | herald::datatype::Data original; |
126 | 1 | herald::datatype::Data result = |
127 | 1 | herald::ble::filter::BLEAdvertParser::subDataBigEndian(original,0,1); |
128 | 1 | REQUIRE(0 == result.size()); |
129 | 1 | } |
130 | 1 | } |
131 | | |
132 | 1 | TEST_CASE("advert-parser-subdatalittleendianemptydata", "[advert][parser][subdatalittleendianemptydata]") { |
133 | 1 | SECTION("advert-parser-subdatalittleendianemptydata") { |
134 | 1 | herald::datatype::Data original; |
135 | 1 | herald::datatype::Data result = |
136 | 1 | herald::ble::filter::BLEAdvertParser::subDataLittleEndian(original,0,1); |
137 | 1 | REQUIRE(0 == result.size()); |
138 | 1 | } |
139 | 1 | } |
140 | | /* |
141 | | |
142 | | // TODO MOVE THESE TO DATA ITSELF |
143 | | @Test |
144 | | public void testDataSubsetBigEndianNullData() throws Exception { |
145 | | byte[] result = BLEAdvertParser.subDataBigEndian(null,0,1); |
146 | | assertNotNull(result); |
147 | | assertEquals(0, result.length); |
148 | | } |
149 | | |
150 | | @Test |
151 | | public void testDataSubsetLittleNullEmptyData() throws Exception { |
152 | | byte[] result = BLEAdvertParser.subDataLittleEndian(null,0,1); |
153 | | assertNotNull(result); |
154 | | assertEquals(0, result.length); |
155 | | } |
156 | | */ |
157 | | |
158 | | // MARK: HIGH LEVEL FULL PACKET METHODS |
159 | | |
160 | 1 | TEST_CASE("advert-parser-appletvfg", "[advert][parser][appletvfg]") { |
161 | 1 | SECTION("advert-parser-appletvfg") { |
162 | 1 | std::uint8_t data[] { |
163 | 1 | 0x02, 0x01, 0x1a, |
164 | 1 | 0x02, 0x0a, 0x08, |
165 | 1 | 0x0c, 0xff, 0x4c, 0x00, |
166 | 1 | 0x10, 0x07, 0x33, |
167 | 1 | 0x1f, 0x2c, 0x30, 0x2f, 0x92, |
168 | 1 | 0x58 |
169 | 1 | }; |
170 | 1 | herald::datatype::Data original(data, 19); |
171 | 1 | REQUIRE(19 == original.size()); |
172 | 1 | REQUIRE("02011a020a080cff4c001007331f2c302f9258" == original.hexEncodedString()); |
173 | 1 | |
174 | 1 | auto result = herald::ble::filter::BLEAdvertParser::extractSegments(original,0); |
175 | 1 | REQUIRE(3 == result.size()); |
176 | 1 | REQUIRE(result[0].type == herald::ble::filter::BLEAdvertSegmentType::flags); |
177 | 1 | REQUIRE(result[1].type == herald::ble::filter::BLEAdvertSegmentType::txPowerLevel); |
178 | 1 | REQUIRE(result[2].type == herald::ble::filter::BLEAdvertSegmentType::manufacturerData); |
179 | 1 | |
180 | 1 | auto manu = herald::ble::filter::BLEAdvertParser::extractManufacturerData(result); |
181 | 1 | REQUIRE(1 == manu.size()); |
182 | 1 | |
183 | 1 | auto manuData = manu.front(); |
184 | 1 | REQUIRE(9 == manuData.data.size()); |
185 | 1 | REQUIRE(std::byte(0x10) == manuData.data.at(0)); |
186 | 1 | REQUIRE(std::byte(0x07) == manuData.data.at(1)); |
187 | 1 | |
188 | 1 | // Check we get an apple manufacturer data from it |
189 | 1 | auto appleData = herald::ble::filter::BLEAdvertParser::extractAppleManufacturerSegments(manu); |
190 | 1 | REQUIRE(1 == appleData.size()); |
191 | 1 | auto appleMD = appleData.front(); |
192 | 1 | REQUIRE(appleMD.data.size() == 7); // not including initial type code, and length value |
193 | 1 | REQUIRE(appleMD.type == 0x10); |
194 | 1 | } |
195 | 1 | } |
196 | | |
197 | 1 | TEST_CASE("advert-parser-heraldpseudoaddress", "[advert][parser][heraldpseudoaddress]") { |
198 | 1 | SECTION("advert-parser-heraldpseudoaddress") { |
199 | 1 | std::uint8_t data[] { |
200 | 1 | 0x02, 0x01, 0x1a, |
201 | 1 | 0x02, 0x0a, 0x08, |
202 | 1 | 0x09, 0xff, 0xff, 0xfa, |
203 | 1 | 0x10, 0x07, 0x33, 0x1f, 0x2c, 0x30 |
204 | 1 | }; |
205 | 1 | herald::datatype::Data original(data, 16); |
206 | 1 | REQUIRE(16 == original.size()); |
207 | 1 | REQUIRE("02011a020a0809fffffa1007331f2c30" == original.hexEncodedString()); |
208 | 1 | |
209 | 1 | auto result = herald::ble::filter::BLEAdvertParser::extractSegments(original,0); |
210 | 1 | REQUIRE(3 == result.size()); |
211 | 1 | REQUIRE(result[0].type == herald::ble::filter::BLEAdvertSegmentType::flags); |
212 | 1 | REQUIRE(result[1].type == herald::ble::filter::BLEAdvertSegmentType::txPowerLevel); |
213 | 1 | REQUIRE(result[2].type == herald::ble::filter::BLEAdvertSegmentType::manufacturerData); |
214 | 1 | |
215 | 1 | auto manu = herald::ble::filter::BLEAdvertParser::extractManufacturerData(result); |
216 | 1 | REQUIRE(1 == manu.size()); |
217 | 1 | |
218 | 1 | auto manuData = manu.front(); |
219 | 1 | REQUIRE(6 == manuData.data.size()); |
220 | 1 | REQUIRE(std::byte(0x10) == manuData.data.at(0)); |
221 | 1 | REQUIRE(std::byte(0x07) == manuData.data.at(1)); |
222 | 1 | |
223 | 1 | // Check we get a herald manufacturer data from it |
224 | 1 | auto heraldDataV = herald::ble::filter::BLEAdvertParser::extractHeraldManufacturerData(manu); |
225 | 1 | REQUIRE(heraldDataV.size() == 1); |
226 | 1 | auto heraldData = heraldDataV.front(); |
227 | 1 | REQUIRE(6 == heraldData.size()); |
228 | 1 | REQUIRE(std::byte(0x10) == heraldData.at(0)); |
229 | 1 | REQUIRE(std::byte(0x30) == heraldData.at(5)); |
230 | 1 | } |
231 | 1 | } |